package com.fadadianqi.logistics.admin.printer;

import com.fadadianqi.commons.ConfigHelper;
import com.yeqiangwei.commons.util.StringHelper;
import org.apache.log4j.Logger;

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;

public class PrintService {

    private static final Logger log = Logger.getLogger(PrintService.class);

    private Paper paper;
    private String content;

    public PrintService(String content, Paper paper) {
        this.content = content;
        this.paper = paper;
    }

    public void doPrint() throws PrinterException {
        String comm = ConfigHelper.getString("printer.comm", "net").trim();
        log.debug("printer.comm: " + comm);
        if ("net".equalsIgnoreCase(comm)) {
            try {
                netPrint();
            } catch (Exception e) {
                log.error(e);
                throw new PrinterException("exception.printer.network.errors");
            }
        } else if ("usb".equalsIgnoreCase(comm)) {
            try {
                localPrint();
            } catch (java.awt.print.PrinterException e) {
                log.error(e);
                throw new PrinterException("exception.printer.local.errors");
            }
        }
    }

    /**
     * 网络打印
     *
     * @throws Exception
     */
    public void netPrint() throws Exception {
        String ip = ConfigHelper.getString("printer.ip");
        EthernetPrinter printer = new EthernetPrinter(ip, 9100);
        String[] linesText = content.split("\n");
        int line = 0;
        for (String text : linesText) {
            log.debug("text: " + text); // 每行文本
            text = text
                    .replaceAll("\\[fz(.*?)\\]", StringHelper.EMPTY)
                    .replaceAll("\\[lh(.*?)\\]", StringHelper.EMPTY);
            if (line == 0) {
                printer.zoomIn();
            } else if (line == 1) {
                printer.zoomOut();
            }
            printer.println(text);
            line++;
        }
        printer.finish();
    }

    /**
     * 本地打印
     *
     * @throws PrinterException
     * @throws java.awt.print.PrinterException
     */
    public void localPrint() throws PrinterException, java.awt.print.PrinterException {
        Book book = new Book(); // 通俗理解就是书、文档
        PageFormat pf = new PageFormat(); // 设置成竖打
        book.append(new LocalPrinter(this.content), pf);

        // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
        pf.setOrientation(PageFormat.PORTRAIT);
        pf.setPaper(paper);

        // 获取打印服务对象
        PrinterJob job = PrinterJob.getPrinterJob();

        // 设置打印类
        job.setPageable(book);
        boolean showDialog = ConfigHelper.getBoolean("printer.dialog.show");
        if (showDialog) {
            // 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
            boolean a = job.printDialog();
            if (a) {
                job.print();
            }
        } else {
            job.print();
        }
    }

}
